﻿using System;
using System.Text.RegularExpressions;

namespace ART.Common
{
    /// <summary>
    /// 验证类
    /// Create by Jinlong Zhang
    /// </summary>
    public class JValidate
    {

        /// <summary>
        /// 判断字符串是否整数
        /// </summary>
        /// <param name="source">要判断的字符串</param>
        public static bool IsInt(string source)
        {
            Regex intRegex = new Regex(@"^(-){0,1}\d+$");
            if (intRegex.Match(source).Success)
            {
                if (long.Parse(source) > int.MaxValue || long.Parse(source) < -2147483648)
                    return false;
                else
                    return true;
            }
            else
                return false;
        }


        /// <summary>
        /// 判断字符串是否正整数
        /// </summary>
        /// <param name="source">要判断的字符串</param>
        public static bool IsPositiveInt(string source)
        {
            Regex intRegex = new Regex(@"^\d+$");
            if (intRegex.Match(source).Success)
            {
                if (long.Parse(source) > int.MaxValue)
                    return false;
                else
                    return true;
            }
            else
                return false;
        }



        /// <summary>
        /// 判断字符串是否负整数
        /// </summary>
        /// <param name="source">要判断的字符串</param>
        public static bool IsNegativeInt(string source)
        {
            Regex intRegex = new Regex(@"^-\d+$");
            if (intRegex.Match(source).Success)
            {
                if (long.Parse(source) < -2147483648)
                    return false;
                else
                    return true;
            }
            else
                return false;
        }


        /// <summary>
        /// 判断字符串是否数字和英文字母
        /// </summary>
        /// <param name="source">要判断的字符串</param>
        public static bool IsWordAndNum(string source)
        {
            Regex intRegex = new Regex(@"[a-zA-Z0-9]?");
            return intRegex.Match(source).Success;
        }


        /// <summary>
        /// 判断字符串是否为URL格式
        /// </summary>
        public static bool IsUrl(string source)
        {
            Regex urlRegex = new Regex(@"(http://)?([\w-]+\.)*[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.IgnoreCase);
            return urlRegex.Match(source).Success;
        }


        /// <summary>
        /// 判断字符串是否为E-Mail格式
        /// </summary>
        public static bool IsEmail(string source)
        {
            Regex emailRegex = new Regex(@"^\w+([-+.]\w+)*@(\w+([-.]\w+)*\.)+([a-zA-Z]+)+$", RegexOptions.IgnoreCase);
            return emailRegex.Match(source).Success;
        }


        /// <summary>
        /// 判断字符串是否为身份证格式(15位或者18位)
        /// </summary>
        public static bool IsIDCard(string source)
        {
            if (source.Length != 15 && source.Length != 18)
                return false;
            if (source.Length == 15)
            {
                Regex idCardRegex = new Regex(@"^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$");
                if (!idCardRegex.Match(source).Success)
                    return false;
                string[] obj = idCardRegex.Split(source);
                try
                {
                    DateTime dt = new DateTime(int.Parse("19" + obj[2]), int.Parse(obj[3]), int.Parse(obj[4]));
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                Regex idCardRegex = new Regex(@"^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9Xx])$");
                if (!idCardRegex.Match(source).Success)
                    return false;
                string[] obj = idCardRegex.Split(source);
                try
                {
                    DateTime dt = new DateTime(int.Parse(obj[2]), int.Parse(obj[3]), int.Parse(obj[4]));
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 判断字符串是否为邮编
        /// </summary>
        public static bool IsPostalCode(string source)
        {
            Regex postalcodeRegex = new Regex(@"^\d{6}$", RegexOptions.IgnoreCase);
            return postalcodeRegex.Match(source).Success;
        }


        /// <summary>
        /// 判断字符串是否为手机号码格式
        /// </summary>
        public static bool IsMobileNum(string source)
        {
            Regex mobileNumRegex = new Regex(@"^1\d{10}$", RegexOptions.IgnoreCase);
            return mobileNumRegex.Match(source).Success;
        }


        /// <summary>
        /// 判断字符串是否为固定电话格式
        /// </summary>
        public static bool IsPhoneNum(string source)
        {
            Regex phoneNumRegex = new Regex(@"^(86)?(-)?(0\d{2,3})?(-)?(\d{7,8})(-)?(\d{3,5})?$", RegexOptions.IgnoreCase);
            return phoneNumRegex.Match(source).Success;
        }



        /// <summary>
        /// 判断字符串是否为指定长度之内
        /// </summary>
        /// <param name="source">输入字符串</param>
        /// <param name="begin">最小长度</param>
        /// <param name="end">最大长度</param>
        public static bool IsLengthStr(string source, int begin, int end)
        {
            int length = source.Length;
            if (length < begin && length > end)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 判断字符串是否为日期格式
        /// </summary>
        public static bool IsDateTime(string source)
        {
            try
            {
                DateTime dt = Convert.ToDateTime(source);
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
